home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 8123 < prev    next >
Encoding:
Text File  |  1996-08-05  |  5.9 KB  |  141 lines

  1. Newsgroups: comp.lang.c
  2. Path: news.sprintlink.net!eskimo!scs
  3. From: scs@eskimo.com (Steve Summit)
  4. Subject: Re: What is &Variable (declared as: char Variable[10])?
  5. X-Nntp-Posting-Host: eskimo.com
  6. Message-ID: <DnLKHM.3wB@eskimo.com>
  7. Sender: news@eskimo.com (News User Id)
  8. Organization: schmorganization
  9. References: <4gtab6$acb@ceylon.gte.com> <313318b8.53776146@nntp.ix.netcom.com> <4h1u9d$sqq@ceylon.gte.com>
  10. Date: Fri, 1 Mar 1996 16:29:46 GMT
  11.  
  12. Brenda, please fix your newsposting software so that your From:
  13. line is usable so that we can reply to you by e-mail rather than
  14. hashing this all out in the newsgroup.  The articles you've been
  15. posting contain the line
  16.  
  17.     From: Brenda <g051286>
  18.  
  19. which is not something I can reply to; it needs to say something
  20. like
  21.  
  22.     From: Brenda <g051286@gte.com>
  23. or
  24.     From: g051286@gte.com (Brenda)
  25.  
  26. (If you're using Netscape, go into the Network/Mail Preferences
  27. setup, and set your e-mail address to a complete Internet e-mail
  28. address, including the @ sign.  Also, complain to Netscape and
  29. GTE that they allowed you to look foolish by posting articles to
  30. which no one could reply.)
  31.  
  32. In article <4h1u9d$sqq@ceylon.gte.com>, Brenda <g051286> writes:
  33. > What is the definition of a pointer?  I have always been taught that a
  34. > pointer is simply an address in memory, and an array name (i.e. myarray)
  35. > is simply a CONSTANT address.
  36.  
  37. These are fuzzy definitions.  Slightly more precise would be to
  38. say that a pointer *value* is an address, that a pointer
  39. *variable* *holds* an address, and that an array *has* a constant
  40. address.
  41.  
  42. Informal definitions (such as the "fuzzy" ones you gave, and the
  43. "slightly more precise" ones I just gave) are fine in some
  44. contexts.  In particular, as I argue in another thread spun off
  45. of this one, it's perfectly reasonable to think of a pointer, as
  46. used in certain contexts, as a kind of "array" (the context
  47. being, of course, when the pointer points to a contiguous block
  48. of elements which you're indexing into).  But it is not useful to
  49. think of an array as a pointer.  An array is not a pointer, and
  50. it doesn't even act like one.  (All of this confusion arises
  51. because pointers are automatically generated when you use arrays
  52. in most expressions.)
  53.  
  54. The other problem with fuzzy definitions is that they tend to
  55. get you into trouble out here in this nonstop flamefest called
  56. Usenet, where some of us (too many of us?) are incorrigible
  57. nitpickers and language lawyers who use ridiculously precise
  58. definitions and bristle at inaccurate ones.
  59.  
  60. > Of course there are differences between arrays and pointers
  61. > due to the fact that an array is a CONSTANT address and a pointer is a
  62. > VARIABLE address.
  63.  
  64. The differences involve much more than that.  When I say
  65.  
  66.     int a, b, *ip;
  67.     if(some_condition)
  68.         ip = &a;
  69.     else    ip = &b;
  70.  
  71. I'm doing something with a pointer which I can never do with an
  72. array, because what I'm doing doesn't even act like an array.
  73. When I say
  74.  
  75.     char line[100];
  76.     fgets(line, (int)sizeof(line), stdin);
  77.  
  78. I'm doing something with an array which I can never do (in quite
  79. that way) with a pointer, because the compiler automatically
  80. allocates space for arrays and (usually) knows how big they are.
  81.  
  82. > And the reason I said you shouldn't (note shouldn't
  83. > not couldn't) say &myarray is:
  84. > ==================================================
  85. > (from "A Book On C", Kelley & Pohl, pg 200)
  86. > Constructs not to be pointed at:
  87. > 1. Do not point at constants. (&3)
  88. > 2. Do not point at arrays; an array name is a constant. (int a[77]; &a)
  89. > 3. Do not point at ordinary expressions &(k + 99)
  90. > 4. Do not point at register variables. (register v; &v)
  91. > The address operator can be applied to variables and array elements.
  92. > ===================================================
  93.  
  94. Okay, I do note that you said "shouldn't" and not "couldn't".
  95. I agree that, the vast majority of the time, you shouldn't write
  96. &a where a is an array, because it usually isn't what you want.
  97. Situations 1, 3, and 4, however, are places where you shouldn't
  98. because you *can't*.  Kelley & Pohl are being a little imprecise
  99. here, too.
  100.  
  101. > So again I say, myarray is DEFINITELY a pointer (i.e. address in memory).
  102.  
  103. We keep trying to tell you, but you just won't listen.
  104. myarray *has* an address in memory, just as all variables
  105. (simple, pointer, or array) do.  If I say
  106.  
  107.     int i, *ip, myarray[10];
  108.  
  109. then i, ip, and myarray all have addresses.  Only ip is a
  110. pointer, and besides the address that it *has*, there is another
  111. address that it can *hold*: the address of the thing that it
  112. points to.  myarray, on the other hand, is in and of itself in no
  113. way shape or form a pointer; it is an array, and that is why we
  114. call it an array.  Due to the "equivalence" of arrays and
  115. pointers in C, when we *use* myarray in most expressions, the
  116. compiler will generate a pointer to its first element.  That
  117. doesn't mean that myarray is a pointer, any more than a saw is a
  118. birdhouse because I can use it to make one, or a camera is a
  119. picture because I can use it to take one.
  120.  
  121. Finally, be careful with that word "address."  I like to call
  122. unary & the "pointer to" operator, not the "address of" operator.
  123. If you're not an assembly language programmer (and the whole
  124. point of using an allegedly higher level language like C is to
  125. avoid having to program in assembler), it is nearly impossible to
  126. think or even speak precisely about the distinction between the
  127. address *of* a pointer variable and the address which the pointer
  128. variable *contains*.  If instead you think of all variables
  129. (simple, array, and pointer) as locations in memory that can hold
  130. values, and that the value of a pointer variable is a pointer to
  131. another location, you won't fall into the trap of thinking that
  132. an array is what a pointer has.
  133.  
  134.                     Steve Summit
  135.                     scs@eskimo.com
  136. -- 
  137. The Communications Decency Act within the Telecommunications Act
  138. of 1996 (U.S.) is an annoying, threatening, abusive, indecent,
  139. and obscene piece of legislation which attempts to ban annoying,
  140. threatening, abusive, indecent, or obscene communication.
  141.